const util = require('../../utils/util.js');
const wxHttpHandler = require('../../utils/wxHttpHandler.js');
const api = require('../../APIConfig/apiConfig.js');

var app = getApp();

Page({
  data: {
    indicatorIndex: 2, //标识(0代表未绑,1代表绑定，2代表全部)
    keyWords: '',
    datas: [],
  },

  onLoad: function (options) {
    this.setData({
      indicatorIndex: options.type,
      keyWords: options.keyWords
    })
  },

  onShow: function () {
    this.queryIndexboundVehicle()
  },


  changeTab: function (e) {
    this.setData({
      indicatorIndex: e.currentTarget.dataset.a
    })
    this.queryIndexboundVehicle()
  },

  keyWordInput: function (e) {
    console.log(e.detail.value)
    if (e.detail.value != '') {
      this.setData({
        keyWords: e.detail.value,
        indicatorIndex: 2
      })
    } else {

      this.setData({
        keyWords: e.detail.value,
        indicatorIndex: 2
      })
      this.queryIndexboundVehicle()
    }

  },

  bindconfirm: function () {
    this.queryIndexboundVehicle()
  },

  toBinndOrUnbind: function (e) {
    let status = e.currentTarget.dataset.b
    let registrationNO = e.currentTarget.dataset.c
    let vehicleId = e.currentTarget.dataset.d
    if (status == 0) { //去绑定
      wx.navigateTo({
        url: '../bind/bind?registrationNO='+registrationNO+'&vehicleId='+vehicleId,
      })
    } else if (status == 1) { //去解绑
      wx.navigateTo({
        url: '../unbind/unbind?registrationNO='+registrationNO+'&vehicleId='+vehicleId,
      })
    }

  },


  //搜索车辆或设备号
  queryIndexboundVehicle: function () {
    let data = {
      uid: wx.getStorageSync('id'),
      vtag: this.data.indicatorIndex,
      serchResult: this.data.keyWords
    }
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.queryIndexboundVehicle,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          that.setData({
            datas: res.data
          })
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },
})